package kr.saeildadim.androidexam_saeildamil.chapter9;

import kr.saeildadim.androidexam_saeildamil.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Mycontextmenu extends Activity {
	Button mBtn;
	EditText mEdit;
	MyImage mImage;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch09_contextmenu);
		mBtn = (Button) findViewById(R.id.ch09_contextmenu_btn01);
		mEdit = (EditText) findViewById(R.id.ch09_cintext_edit);
		mImage = (MyImage) findViewById(R.id.ch09_contextmenu_myimage);
		
		registerForContextMenu(mBtn);
		registerForContextMenu(mEdit);
		registerForContextMenu(mImage);
		
		
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		
		if(v == mBtn){
			menu.setHeaderIcon(R.drawable.ic_launcher).setHeaderTitle("Button Menu");
			menu.add(0, 1, 0, "RED");
			menu.add(0, 2, 0, "GREEN");
			menu.add(0, 3, 0, "BLUE");
			
		}
	
		if(v==mEdit){
			menu.setHeaderTitle("Edit menu");
			menu.add(0, 4, 0, "번역하기");
			menu.add(0, 5, 0, "필기인식");
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		super.onContextItemSelected(item);
		switch(item.getItemId()){
			case 1:
				mBtn.setTextColor(Color.RED);
				return true;
			case 2:
				mBtn.setTextColor(Color.GREEN);
				return true;
			case 3:
				mBtn.setTextColor(Color.BLUE);
				return true;
			case 4:
				Toast.makeText(this, "번역했다", Toast.LENGTH_LONG).show();
				return true;
			case 5:
				Toast.makeText(this, "필기인식", Toast.LENGTH_LONG).show();
				return true;
			case 100:
				Toast.makeText(this, "회전", Toast.LENGTH_LONG).show();
				Bitmap bit1 = BitmapFactory.decodeResource(getResources(), R.drawable.huysung);
				Matrix mat1 = new Matrix();
				mat1.preScale(1, -1f);
				Bitmap bitmap1 = Bitmap.createBitmap(bit1, 0,0, bit1.getWidth(), bit1.getHeight(), mat1, false);
				mImage.setImageBitmap(bitmap1);
				return true;
			case 101:
				Toast.makeText(this, "크기변경", Toast.LENGTH_LONG).show();
				Bitmap bit = BitmapFactory.decodeResource(getResources(), R.drawable.huysung);
				Matrix mat = new Matrix();
				mat.preScale(2, 2);
				Bitmap bitmap = Bitmap.createBitmap(bit, 0,0, bit.getWidth(), bit.getHeight(), mat, false);
				mImage.setImageBitmap(bitmap);
				return true;
				
		
		}
		
		return false;
	}

}
